﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
namespace TinyCinema.Data
{
    public class Position : Execution
    {
        public string ID { get; set; }
        private bool isInit = true;
        public string Name 
        { 
            get
            {
                return _Name;
            } 
            set
            {
                bool check = false;
                if (_Name != null) check = true;
                _Name = value;
                if (check && !isInit)
                    Save();
            }
        }
        public bool IsManager { get; set; }
        private string _Name;
        public ObservableCollection<Employee> Employees { get; set; }
        public Position()
        {
            IsManager = false;
            ID = IDGenerate(Tables.Position);
        }
        public Position(int index)
        {
            Index = index;
        }
        public Position(string _ID)
        {
            ID = _ID;
            PutTableIntoDataSet(Tables.Position);
            using (var tbl = ds.Tables[Tables.Position])
            {
                var Row = tbl.Select(PositionTable.ID + "='" + ID + "'")[0];
                Name = Row[PositionTable.Name] as string;
                try
                {
                    IsManager = (bool)Row[PositionTable.IsManager];
                }
                catch { }
                if (lstPosition == null)
                {
                    lstPosition = new ObservableCollection<Position>();
                    lstPosition = GetAllPositions();
                }
                for (int i = 0; i < lstPosition.Count; ++i)
                    if (ID == lstPosition[i].ID) Index = i;
            }
            PutTableIntoDataSet(Tables.Permission);
            Permission = new Permisson(ID);
            isInit = false;
        }
        public static ObservableCollection<Position> lstPosition { get; set; }
        int _Index { get; set; }
        public int Index
        {
            get { return _Index; }
            set
            {
                ID = lstPosition[value].ID;
                Name = lstPosition[value].Name;
                IsManager = lstPosition[value].IsManager;
                _Index = value;
            }
        }
        #region Các quyền
        public Permisson Permission { get; set; }
        #endregion
        public static ObservableCollection<Position> GetAllPositions()
        {
            var lst = new ObservableCollection<Position>();
            PutTableIntoDataSet(Tables.Position);
            var tbl = ds.Tables[Tables.Position];
            var Rows = tbl.Select();
            lst = new ObservableCollection<Position>();
            foreach (var row in Rows)
                lst.Add(new Position(row[PositionTable.ID] as String));
            return lst;
        }
        public void Save()
        {
            //using (var tbl = ds.Tables[Tables.Position])
            //{
            //    if (ID == null)
            //    {
                    
            //        var Row = tbl.NewRow();
            //        Row[PositionTable.ID] = ID;
            //        Row[PositionTable.Name] = Name;
            //        Row[PositionTable.IsManager] = IsManager;
            //        tbl.Rows.Add(Row);

            //        SaveCommand(Tables.Position);
            //    }
            //    else
            //    {
            //        foreach (System.Data.DataRow Row in tbl.Rows)
            //            if ((Row[PositionTable.ID] as String) == ID)
            //            {
            //                Row[PositionTable.Name] = Name;
            //                Row[PositionTable.IsManager] = IsManager;

            //                SaveCommand(Tables.Position);
            //                break;
            //            }
            //    }
            //}
            var tbl = ds.Tables[Tables.Position];
            var Rows = tbl.Select(PositionTable.ID + "='" + ID + "'");
            if (Rows.Length == 0) //đang tạo mới
            {
                var Row = tbl.NewRow();
                Row[PositionTable.ID] = ID;
                Row[PositionTable.Name] = Name;
                Row[PositionTable.IsManager] = IsManager;
                tbl.Rows.Add(Row);
                SaveCommand(Tables.Position);
            }
            else
            {
                var Row = Rows[0];
                Row[PositionTable.Name] = Name; 
                Row[PositionTable.IsManager] = IsManager;
                SaveCommand(Tables.Department);
            }
            lstPosition = GetAllPositions();
            OnPropertyChanged("lstPositions");
        }
        public void Delete() 
        {
            using (var tbl = ds.Tables[Tables.Employee])
            {
                var Row = tbl.Select(EmployeeTable.PositionID + "='" + ID + "'");
                if (Row.Length > 0) throw new Exception();
            }
            using (var tbl = ds.Tables[Tables.Permission])
            {
                var Rows = tbl.Select(PermissionTable.PositionID + "='" + ID + "'");
                foreach(var Row in Rows) Row.Delete();
                SaveCommand(Tables.Permission);
            }
            using(var tbl = ds.Tables[Tables.Position])
            {
                var Row = tbl.Select(PositionTable.ID + "='" + ID + "'")[0];
                Row.Delete();
                SaveCommand(Tables.Position);
            }
            
        }

    }
}
